package com.yeyks.consumer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.yeyks.common.em.consumer.UserInviteRelationTypeEnum;
import com.yeyks.common.param.PageParam;
import com.yeyks.common.result.activity.ActivityCardStatResult;
import com.yeyks.common.result.activity.ActivityUserInviteChildResult;
import com.yeyks.common.result.user.UserDataStatInviteSortResult;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.dal.domain.UserInviteRelation;
import com.yeyks.consumer.dal.dao.UserInviteRelationMapper;
import com.yeyks.consumer.service.UserInviteRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 用户邀请关系表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-09-16
 */
@Service
public class UserInviteRelationServiceImpl extends ServiceImpl<UserInviteRelationMapper, UserInviteRelation> implements UserInviteRelationService {

    //存储用户关系
    @Override
    public boolean createUserInviteRelation(Integer userId, Integer parentId, Integer type) {
        UserInviteRelation userInviteRelation = new UserInviteRelation();
        userInviteRelation.setUserId(userId);
        userInviteRelation.setParentId(parentId);
        userInviteRelation.setType(type);
        return save(userInviteRelation);
    }

    /**
     * 通过用户id查询上级Id
     *
     * @param userId
     * @param type   {@link UserInviteRelationTypeEnum}
     * @return
     */
    @Override
    public Integer selectParentIdByUserId(Integer userId, Integer type) {
        UserInviteRelation userInviteRelation = baseMapper.selectOne(new QueryWrapper<UserInviteRelation>()
                .eq(UserInviteRelation.TYPE, type)
                .eq(UserInviteRelation.USER_ID, userId)
                .last("limit 1"));
        if (null == userInviteRelation) {
            return null;
        }
        return userInviteRelation.getParentId();
    }

    /**
     * 邀请人数排名
     *
     * @param type 1:0元邀请 2:创客邀请 3:周杰伦活动用户
     * @return
     */
    @Override
    public List<UserDataStatInviteSortResult> inviteSort(Integer type) {
        List<UserDataStatInviteSortResult> sortResults = baseMapper.selectInviteSort(type);
        return sortResults == null ? Lists.newArrayList() : sortResults;
    }

    /**
     * 获取邀请人数
     *
     * @param types    {@link com.yeyks.common.em.consumer.UserInviteRelationTypeEnum}
     * @param parentId 上级用户id
     * @return
     */
    @Override
    public Integer getInviteUserCount(List<Integer> types, Integer parentId) {
        Integer count = baseMapper.selectCount(new QueryWrapper<UserInviteRelation>()
                .in(UserInviteRelation.TYPE, types)
                .eq(UserInviteRelation.PARENT_ID, parentId));
        return null == count ? 0 : count;
    }

    /**
     * 新用户数
     *
     * @param type {@link com.yeyks.common.em.consumer.UserInviteRelationTypeEnum}
     * @return
     */
    @Override
    public Integer getNewUserCount(Integer type) {
        Integer count = baseMapper.selectCount(new QueryWrapper<UserInviteRelation>()
                .eq(UserInviteRelation.TYPE, type));
        return null == count ? 0 : count;
    }

    /**
     * 根据类型统计邀请人
     *
     * @param type {@link com.yeyks.common.em.consumer.UserInviteRelationTypeEnum}
     * @return
     */
    @Override
    public List<ActivityCardStatResult> stat(Integer type) {
        return baseMapper.stat(type);
    }

    @Override
    public String getParentPhone(Integer userId) {
        return baseMapper.getParentPhone(userId);
    }
}
